﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{
    class class_ISYC_201_06
    {
        public static void ISYC_201_06(Dictionary<string, Object> data) //五角刀片
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中性线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //基本参数-------------------------------------------------------------------------------------------------
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            //赋值参数--------------------------------------------------------------------------------------------------
            double IC = Convert.ToDouble(data["ic"]);
            double EPSR = Convert.ToDouble(data["epsr"]);
            double EPSR1 = Convert.ToDouble(data["epsr1"]);//六角
            double EPSR2 = Convert.ToDouble(data["epsr2"]);//八角
            double L = IC / 2.0 / Math.Tan((EPSR / 2.0) * pi / 180) * 2;
            double L1 = IC / 2.0 / Math.Tan((EPSR1 / 2.0) * pi / 180) * 2;//六角
            double L2 = IC / 2.0 / Math.Tan((EPSR2 / 2.0) * pi / 180) * 2;//八角
            double S = Convert.ToDouble(data["s"]);
            double AN = Convert.ToDouble(data["an"]);
            double RE = Convert.ToDouble(data["re"]);
            double scale = L / 10;//放大系数

            //点---------------------------------------------------------------------------------------------
            //第一部分
            //轮廓
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = fun.Polar(v1, -((90 - EPSR / 2.0) + 90) * pi / 180, IC / 2.0 / Math.Sin(EPSR / 2.0 * pi / 180));
            Vector2 v3 = fun.Polar(v2, EPSR * pi / 180, L / 2.0);
            Vector2 v4 = fun.Polar(v2, EPSR * pi / 180, L);
            Vector2 v5 = fun.rotatePoint_Changed(v1, v4, -72);
            Vector2 v6 = fun.rotatePoint_Changed(v1, v5, -72);
            Vector2 v7 = fun.Polar(v2, 0 * pi / 180, L);
            Vector2 v8 = fun.Polar(v7, 0 * pi / 180, L * 2);
            Vector2 v9 = fun.Polar(v8, (180 - AN) * pi / 180, S / Math.Cos(AN * pi / 180));
            Vector2 v11 = new Vector2(v8.X, v8.Y + IC);
            Vector2 v10 = fun.Inters(v9, fun.Polar(v9, 90 * pi / 180, L), v11, fun.Polar(v11, -(180 - AN) * pi / 180, L));
            Vector2 v12 = new Vector2(v8.X, v1.Y);

            //轮廓（六角）
            Vector2 l1 = new Vector2(v1.X + L * 3, v1.Y);
            Vector2 l2 = fun.Polar(l1, -((90 - EPSR1 / 2.0) + 90) * pi / 180, IC / 2.0 / Math.Sin(EPSR1 / 2.0 * pi / 180));
            Vector2 l3 = fun.Polar(l2, EPSR1 * pi / 180, L1 / 2.0);
            Vector2 l4 = fun.Polar(l2, EPSR1 * pi / 180, L1);
            Vector2 l5 = fun.rotatePoint_Changed(l1, l4, -60);
            Vector2 l6 = fun.rotatePoint_Changed(l1, l5, -60);
            Vector2 l7 = fun.rotatePoint_Changed(l1, l6, -60);
            Vector2 l8 = fun.Polar(l2, 0 * pi / 180, L1);
            Vector2 l9 = fun.Polar(l8, 0 * pi / 180, L1 * 2);
            Vector2 l10 = fun.Polar(l9, (180 - AN) * pi / 180, S / Math.Cos(AN * pi / 180));
            Vector2 l12 = new Vector2(l9.X, l9.Y + IC);
            Vector2 l11 = fun.Inters(l10, fun.Polar(l10, 90 * pi / 180, L), l12, fun.Polar(l12, -(180 - AN) * pi / 180, L1));
            Vector2 l13 = new Vector2(l9.X, l1.Y);

            //轮廓（八角）
            Vector2 b1 = new Vector2(l1.X + L * 3, l1.Y);
            Vector2 b2 = fun.Polar(b1, -((90 - EPSR2 / 2.0) + 90) * pi / 180, IC / 2.0 / Math.Sin(EPSR2 / 2.0 * pi / 180));
            Vector2 b3 = fun.Polar(b2, EPSR2 * pi / 180, L2 / 2.0);
            Vector2 b4 = fun.Polar(b2, EPSR2 * pi / 180, L2);
            Vector2 b5 = fun.rotatePoint_Changed(b1, b4, -45);
            Vector2 b6 = fun.rotatePoint_Changed(b1, b5, -45);
            Vector2 b7 = fun.rotatePoint_Changed(b1, b6, -45);
            Vector2 b8 = fun.rotatePoint_Changed(b1, b7, -45);
            Vector2 b9 = fun.rotatePoint_Changed(b1, b8, -45);
            Vector2 b10 = fun.Polar(b2, 0 * pi / 180, L2);
            Vector2 b11 = fun.Polar(b10, 0 * pi / 180, L2 * 4);
            Vector2 b12 = fun.Polar(b11, (180 - AN) * pi / 180, S / Math.Cos(AN * pi / 180));
            Vector2 b14 = new Vector2(b11.X, b11.Y + IC);
            Vector2 b13 = fun.Inters(b12, fun.Polar(b12, 90 * pi / 180, L2), b14, fun.Polar(b14, -(180 - AN) * pi / 180, L2));
            Vector2 b15 = new Vector2(b11.X, b1.Y);

            //作图部分-----------------------------------------------------------------------
            //第一部分
            //轮廓
            LwPolyline polyline1 = new LwPolyline();
            polyline1.Vertexes.Add(new LwPolylineVertex(v3));
            polyline1.Vertexes.Add(new LwPolylineVertex(v4));
            polyline1.Vertexes.Add(new LwPolylineVertex(v5));
            polyline1.Vertexes.Add(new LwPolylineVertex(v6));
            polyline1.Vertexes.Add(new LwPolylineVertex(v7));
            polyline1.Vertexes.Add(new LwPolylineVertex(v2));
            polyline1.Vertexes.Add(new LwPolylineVertex(v3));
            fun.PolyFill(dxf, polyline1, 0.1, contour);

            //倒角
            var arc1 = Pus.Fillet4LwPline(polyline1, 1, RE);
            var arc2 = Pus.Fillet4LwPline(arc1, 3, RE);
            var arc3 = Pus.Fillet4LwPline(arc2, 5, RE);
            var arc4 = Pus.Fillet4LwPline(arc3, 7, RE);
            var arc5 = Pus.Fillet4LwPline(arc4, 9, RE);
            fun.PolyFill(dxf, arc5, 0.3, contour);

            //虚线
            Circle circle3 = new Circle(v1, IC / 2.0);
            circle3.Layer = refin;
            circle3.LinetypeScale = 5;
            circle3.Linetype = Linetype.Dashed;
            dxf.AddEntity(circle3);

            //轮廓(六角)
            LwPolyline polyline2 = new LwPolyline();
            polyline2.Vertexes.Add(new LwPolylineVertex(l3));
            polyline2.Vertexes.Add(new LwPolylineVertex(l4));
            polyline2.Vertexes.Add(new LwPolylineVertex(l5));
            polyline2.Vertexes.Add(new LwPolylineVertex(l6));
            polyline2.Vertexes.Add(new LwPolylineVertex(l7));
            polyline2.Vertexes.Add(new LwPolylineVertex(l8));
            polyline2.Vertexes.Add(new LwPolylineVertex(l2));
            polyline2.Vertexes.Add(new LwPolylineVertex(l3));
            fun.PolyFill(dxf, polyline2, 0.05, contour);

            //倒角
            var arc1_1 = Pus.Fillet4LwPline(polyline2, 1, RE);
            var arc2_1 = Pus.Fillet4LwPline(arc1_1, 3, RE);
            var arc3_1 = Pus.Fillet4LwPline(arc2_1, 5, RE);
            var arc4_1 = Pus.Fillet4LwPline(arc3_1, 7, RE);
            var arc5_1 = Pus.Fillet4LwPline(arc4_1, 9, RE);
            var arc6_1 = Pus.Fillet4LwPline(arc5_1, 11, RE);
            fun.PolyFill(dxf, arc6_1, 0.3, contour);

            //虚线
            Circle circle2 = new Circle(l1, IC / 2.0);
            circle2.Layer = refin;
            circle2.LinetypeScale = 5;
            circle2.Linetype = Linetype.Dashed;
            dxf.AddEntity(circle2);

            //轮廓（八角）
            LwPolyline polyline3 = new LwPolyline();
            polyline3.Vertexes.Add(new LwPolylineVertex(b3));
            polyline3.Vertexes.Add(new LwPolylineVertex(b4));
            polyline3.Vertexes.Add(new LwPolylineVertex(b5));
            polyline3.Vertexes.Add(new LwPolylineVertex(b6));
            polyline3.Vertexes.Add(new LwPolylineVertex(b7));
            polyline3.Vertexes.Add(new LwPolylineVertex(b8));
            polyline3.Vertexes.Add(new LwPolylineVertex(b9));
            polyline3.Vertexes.Add(new LwPolylineVertex(b10));
            polyline3.Vertexes.Add(new LwPolylineVertex(b2));
            polyline3.Vertexes.Add(new LwPolylineVertex(b3));
            fun.PolyFill(dxf, polyline3, 0.05, contour);

            //倒角
            var arc1_b = Pus.Fillet4LwPline(polyline3, 1, RE);
            var arc2_b = Pus.Fillet4LwPline(arc1_b, 3, RE);
            var arc3_b = Pus.Fillet4LwPline(arc2_b, 5, RE);
            var arc4_b = Pus.Fillet4LwPline(arc3_b, 7, RE);
            var arc5_b = Pus.Fillet4LwPline(arc4_b, 9, RE);
            var arc6_b = Pus.Fillet4LwPline(arc5_b, 11, RE);
            var arc7_b = Pus.Fillet4LwPline(arc6_b, 13, RE);
            var arc8_b = Pus.Fillet4LwPline(arc7_b, 15, RE);
            fun.PolyFill(dxf, arc6_b, 0.3, contour);

            //虚线
            Circle circle1 = new Circle(b1, IC / 2.0);
            circle1.Layer = refin;
            circle1.LinetypeScale = 5;
            circle1.Linetype = Linetype.Dashed;
            dxf.AddEntity(circle1);

            //标注---------------------------------------------------------------------------
            double height = 1 * scale;
            double arrowsize = 0.5 * scale;
            double exline = 0.5 * scale;

            //IC
            Vector2 bz3 = fun.getPointForArcandLine(v1, fun.Polar(v1, 0 * pi / 180, IC), v1, new Arc(v1, IC / 2.0, 0, 360));
            Vector2 bz4 = fun.getPointForArcandLine(v1, fun.Polar(v1, 180 * pi / 180, IC), v1, new Arc(v1, IC / 2.0, 0, 360));
            fun.dimrot1(dxf, bz3, bz4, 180, L * 2, 0.1, height, arrowsize, exline, "IC", "", "");

            //L
            fun.dimrot1(dxf, v2, v7, 180, L * 0.4, 0.1, height, arrowsize, exline, "L", "", "");

            //IC(六角)
            Vector2 bz1 = fun.getPointForArcandLine(l1, fun.Polar(v1, 0 * pi / 180, IC), l1, new Arc(v1, IC / 2.0, 0, 360));
            Vector2 bz2 = fun.getPointForArcandLine(l1, fun.Polar(v1, 180 * pi / 180, IC), l1, new Arc(v1, IC / 2.0, 0, 360));
            fun.dimrot1(dxf, bz1, bz2, 180, L * 2, 0.1, height, arrowsize, exline, "IC", "", "");

            //L
            fun.dimrot1(dxf, l2, l8, 180, L * 0.4, 0.1, height, arrowsize, exline, "L", "", "");

            //角度标注
            //EPSR
            fun.jdbz2(dxf, new Line(v6, fun.Polar(v6, fun.CalAngle(v5, v6), 0.1)), new Line(v6, fun.Polar(v6, fun.CalAngle(v7, v6), 0.1)), S, height, arrowsize, exline, "EPSR", "", "");

            //EPSR（六角）
            fun.jdbz2(dxf, new Line(l5, fun.Polar(l5, fun.CalAngle(l4, l5), 0.1)), new Line(l5, fun.Polar(l5, 180 * pi / 180, 0.1)), S, height, arrowsize, exline, "EPSR", "", "");

            //IC（八角）
            Vector2 bz1_b = fun.getPointForArcandLine(b1, fun.Polar(b1, 0 * pi / 180, IC), b1, new Arc(b1, IC / 2.0, 0, 360));
            Vector2 bz2_b = fun.getPointForArcandLine(b1, fun.Polar(b1, 180 * pi / 180, IC), b1, new Arc(b1, IC / 2.0, 0, 360));
            fun.dimrot1(dxf, bz1_b, bz2_b, 180, L * 2, 0.1, height, arrowsize, exline, "IC", "", "");

            //L
            fun.dimrot1(dxf, b2, b10, 180, L * 0.4, 0.1, height, arrowsize, exline, "L", "", "");

            //EPSR
            fun.jdbz2(dxf, new Line(b6, fun.Polar(b6, fun.CalAngle(b5, b6), 0.1)), new Line(b6, fun.Polar(b6, 180 * pi / 180, 0.1)), S, height, arrowsize, exline, "EPSR", "", "");

            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }

            //定义格式，根据路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}
